#########################################################################
#									#
# Name:	 	swap_check.pm (Perl Module)				#
#									#
# Completed:	24th September, 2004.					#
#									#
# Updated:	27th September, 2004.					#
#									#
# Author:	Simon Edwards, Proprius Consulting Ltd.			#
#									#
# Arguments:	[Min]							#
#		Min	The minimum amount of swap which must be 	#
#			available, specified in Kb.			#
#			If not specified will default to 51200 (50Mb)	#
#									#
# Purpose:	This module for LEMS is designed to check the status	#
#		of available free swap space, and if it drops below the	#
#		threshold it will return 1, allowing the application	#
#		to be failed over, or an alert raised.			#
#									#
#		The typical lems.local.xml file entry might look	#
#		similar to the following:				#
#									#
#        <check>							#
#                <name>swap_checck</name>				#
#                <type>internal</type>					#
#                <module>swap_check 10240</module>			#
#                <interval>60</interval>				#
#                <action_list>						#
#                        <action rc="0" action="NOP"/>			#
#                        <action rc="1" action="FAILOVER"/>		#
#                </action_list>						#
#        </check>							#
#									#
# Limitations:								#
#									#
# Environment:	None.							#
#									#
# Exit Codes:	N/A							#
# 									#
# Version:	@(#)1.0.0 Original (SE)>				#
# 		@(#)1.0.1 Small bug correction (SE)>			#
#									#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#									#
# Complete SOCM Version History						#
#									#
# Version:	1.0.0							#
# Date:		24th September, 2004.					#
# Author:	Simon Edwards, Proprius Consulting Ltd.			#
# Change:	Original.						#
#									#
#########################################################################

package swap_check;

sub new {
my $self={};
my $lim;

	if(exists($_[1]) && defined($_[1])) {
		$lim=$_[1];
	} else {
		$lim=51200;
	}

	$self->{LIMIT}=$lim;
	bless($self);
	return $self;
}

sub check {
my $self=shift;
my ($fd);

	#################################################################
	# Read in the memory information extracting required field 	#
	# from the line:						#
	# SwapFree:    NNN Kb						#
	#################################################################

	open($fd,"/proc/meminfo") or return(0);
	while(<$fd>) {
		next if ! /^SwapFree:\s+([0-9]+)/;
		close($fd);
		$self->{LAST}=$1;
		if($1 < $self->{LIMIT}) {
			return 1;
		} else {
			return 0;
		}
	}
	close($fd);
	return 0;
}

sub stat {
my $self=shift;

	if(exists($self->{LAST})) {
		return $self->{LAST} . "," . $self->{LIMIT};
	}
	return "";
}
1;
